/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import "UIConstants.js" as UIConstants

Item {
    id: root

    signal released
    signal holded

    property int titleSize: UI.LIST_TILE_SIZE
    property int titleWeight: Font.Bold
    property color titleColor: UI.LIST_TITLE_COLOR

    property int subtitleSize: UI.LIST_SUBTILE_SIZE
    property int subtitleWeight: Font.Light
    property color subtitleColor: UI.LIST_SUBTITLE_COLOR

    property Component rightHelperItem: null
    property int rightMargin: UIConstants.DEFAULT_MARGIN
    property int rightHelperSpacing: UIConstants.DEFAULT_MARGIN
    property Component leftHelperItem: null
    property int leftMargin: 28
    property int leftHelperSpacing: UIConstants.DEFAULT_MARGIN

    height: UI.LIST_ITEM_HEIGHT
    width: parent.width


    BorderImage {
        id: background
        anchors.fill: parent
        // Fill page porders
        anchors.leftMargin: -UI.MARGIN_XLARGE
        anchors.rightMargin: -UI.MARGIN_XLARGE
        visible: mouseArea.pressed
        source: "image://theme/meegotouch-list-background-pressed-center"
    }

    HorizontalLine {
        id: separator
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.leftMargin: UI.MARGIN_XLARGE
        anchors.right: parent.right
        anchors.rightMargin: UI.MARGIN_XLARGE
        visible: index
    }
    Item {
        id: conents
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.leftMargin: root.leftMargin
        anchors.right: parent.right
        anchors.rightMargin: root.rightMargin
        Loader {
            id: leftHelperLoader
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            sourceComponent: root.leftHelperItem
            onLoaded: item.model = model;
        }
        Image {
            id: iconImage
            anchors.left: leftHelperLoader.left
            anchors.leftMargin: leftHelperLoader.progress ? root.leftHelperSpacing : 0
            anchors.verticalCenter: parent.verticalCenter
            visible: model.iconSource
            width:  iconImage.visible ? UI.LIST_ICON_SIZE : 0
            height: UI.LIST_ICON_SIZE
            source: model.iconSource
            asynchronous: true
        }
        Column {
            anchors.left: iconImage.right
            anchors.leftMargin: iconImage.visible ? UI.LIST_ITEM_SPACING : 0
            anchors.right: rightHelperLoader.left
            anchors.rightMargin: rightHelperLoader.progress ? root.rightHelperSpacing : 0
            anchors.verticalCenter: parent.verticalCenter
            Label {
                id: mainText
                text: model.title
                font.weight: root.titleWeight
                font.pixelSize: root.titleSize
                color: root.titleColor
                width: parent.width
                elide: Text.ElideRight
                wrapMode: Text.Wrap
                maximumLineCount: 2
            }
            Label {
                id: subText
                text: model.subtitle
                font.weight: root.subtitleWeight
                font.pixelSize: root.subtitleSize
                color: root.subtitleColor
                width: parent.width
                visible: text.length
                maximumLineCount: 1
                elide: Text.ElideRight
                wrapMode: Text.NoWrap
            }
        }
        Loader {
            id: rightHelperLoader
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            sourceComponent: root.rightHelperItem
            onLoaded: item.model = model;
        }
    }
    MouseArea {
        id: mouseArea;
        anchors.fill: parent
        onReleased: {
            if(!mouse.wasHeld)
                root.released();
        }
        onPressAndHold: {
            root.holded();
        }
    }
}
